import { computed, ref } from 'vue';
import { type ITableColumnsItem } from '../types';

export const useTableColumns = <T>(cb: () => Array<ITableColumnsItem<T>>) => {
  const _columnsArr = ref(cb());

  const columns = computed({
    get: () => {
      const arr = _columnsArr.value?.map((item) => {
        if (item.isHidden === undefined) item.isHidden = false;
        return item;
      });
      return arr;
    },
    set: (cols) => {
      _columnsArr.value = cols;
    },
  });

  return {
    columns,
  };
};
